package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.ShopService;
import com.cyz.common.bean.Shop;
import com.cyz.common.dto.ShopDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.ShopMapper;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author 34928
* @description 针对表【shop】的数据库操作Service实现
* @createDate 2023-02-21 20:29:55
*/
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop>
    implements ShopService {

    @Override
    public R queryShops() {
        //创建筛选器
        LambdaQueryWrapper<Shop> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据置顶排序
        lambdaQueryWrapper.orderByDesc(Shop::getIsTop);
        //根据是否在打折排序
        lambdaQueryWrapper.orderByDesc(Shop::getIsDiscount);
        //根据更新时间排序
        lambdaQueryWrapper.orderByDesc(Shop::getUpdateTime);
        //查询相关课程
        List<Shop> list = list(lambdaQueryWrapper);
        //返回数据
        return R.success(list);
    }

    @Override
    public R updateShop(ShopDto shopDto) {
        //dto转化
        Shop shop = BeanCopyUtil.copyBean(shopDto, Shop.class);
        //更新数据
        boolean flag = updateById(shop);
        //错误判断
        if(flag){
            return queryShops();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }

    @Override
    public R addShop(ShopDto shopDto) {
        //dto转化
        Shop shop = BeanCopyUtil.copyBean(shopDto, Shop.class);
        //更新数据
        boolean flag = save(shop);
        //错误判断
        if(flag){
            return queryShops();
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    @Override
    public R deleteShops(Integer[] ids) {
        //删除数据
        boolean flag = removeByIds(Arrays.asList(ids));
        //错误判断
        if(flag){
            return queryShops();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }
}




